from django.shortcuts import render, redirect
# 导包
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
# 导入类视图
from django.views import View

from myapp.models import User
import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
# 导入加密库
import hashlib
# 导入图片库
# 绘画库
from PIL import ImageDraw
# 字体库
from PIL import ImageFont
# 图片库
from PIL import Image
# 随机库
import random
# 文件流
import io

import requests

# 导入上传文件夹配置
from mydjango.settings import UPLOAD_ROOT
import os

# 导入原生sql模块
from django.db import connection

import jwt

# 导入redis数据库
import redis

# 导入时间模块
import time

# 导入公共目录变量
from mydjango.settings import BASE_DIR

# 导包
from django.db.models import Q, F

# 导入dwebsocket的库
from dwebsocket.decorators import accept_websocket
import uuid


# 方法视图
def myindex(request):
    return HttpResponse('这里是首页')


class MyView(View):
    # cbv类视图
    greeting = "你好"

    def get(self, request):
        title = "你好,Django"
        num = 100
        li = [1, 2, 3, 4, 5]
        dic = {'name': '汽车', 'price': 100}

        user = User.objects.all()

        # 使用模板渲染
        return render(request, 'index.html', locals())
        # 使用httpresponse返回
        # return HttpResponse(self.greeting)


# 后台用户表添加
class AddUser(View):
    # get
    def get(self, request):
        return render(request, 'adduser.html', locals())

    # post
    def post(self, request):
        username = request.POST.get('username', '未找到用户名')
        password = request.POST.get('password', '未找到密码')
        # img = request.POST.get('img','未找到图片')
        type = request.POST.get('type', '未找到类别')

        print(username, ' ', password, ' ', type)

        # 排重
        user = User.objects.filter(username=username).first()

        if user:
            return HttpResponse('用户已存在,请勿重复注册')
        else:
            user = User(username=username, password=password, img='', type=type)
            user.save()
            return HttpResponse('用户注册成功')


# 后台用户表修改
class UpdateUser(View):
    def get(self, request):
        return render(request, 'updateuser.html', locals())

    def post(self, request):
        id = int(request.GET.get('id'))
        print(id)
        username = request.POST.get('username', '未找到用户名')
        password = request.POST.get('password', '未找到密码')
        # img = request.POST.get('img','未找到图片')
        type = request.POST.get('type', '未找到类别')

        print(username, ' ', password, ' ', type)
        try:
            user = User.objects.filter(id=id).first()

            user.username = username
            user.password = password
            # user.img = img
            user.type = type

            user.save()

            return HttpResponse("用户信息修改成功")

        except Exception as e:
            print(e)
            return HttpResponse('未查询到匹配用户,修改失败')

# 后台用户表删除
class DeleteUser(View):
    def get(self,request):
        id = request.GET.get('id')
        try:
            User.objects.filter(id=id).delete()
            return HttpResponse('用户删除成功')
        except Exception as e:
            print(e)
            return HttpResponse('用户删除失败')
